package com.yuncool.controller.v1;

import com.github.pagehelper.PageInfo;
import com.yuncool.ApiMessage;
import com.yuncool.ServiceResult;
import com.yuncool.constants.ResultCode;
import com.yuncool.domain.BbsComment;
import com.yuncool.domain.YuncoolBbs;
import com.yuncool.domain.vo.BbsRequestVO;
import com.yuncool.domain.vo.CommentVO;
import com.yuncool.service.BbsCommentService;
import com.yuncool.service.CommonTagService;
import com.yuncool.service.YuncoolBbsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther XIE
 * @Date 2019/9/1 17:26
 * @Description TODO
 * @Version 1.0
 */
@RestController
@RequestMapping("index/bbs")
@Api(tags = "社区接口")
@Slf4j
public class BbsController {

	@Autowired
	YuncoolBbsService yuncoolBbsService;
	@Autowired
	BbsCommentService bbsCommentService;
	@Autowired
	CommonTagService commonTagService;

	@ApiOperation(value = "获取帖子列表接口", httpMethod = "POST")
	@PostMapping("listBbs")
	public ServiceResult<PageInfo<YuncoolBbs>> listBbs(@RequestBody BbsRequestVO vo) {
		ServiceResult<PageInfo<YuncoolBbs>> result = new ServiceResult<>();
		try {
			result.setResultData(yuncoolBbsService.listYunCoolBbs(vo));
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(ResultCode.REQUEST_ERROR, ApiMessage.getMessage("MSG001"));
		}
		return result;
	}

	@ApiOperation(value = "获取帖子详情接口", httpMethod = "POST")
	@PostMapping("getBbs")
	@PreAuthorize("hasRole('USER')")
	public ServiceResult<YuncoolBbs> getBbs(@ApiParam(value = "帖子Id") Integer bbsId) {
		ServiceResult<YuncoolBbs> result = new ServiceResult<>();
		try {
			result.setResultData(yuncoolBbsService.getBbs(bbsId));
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(ResultCode.REQUEST_ERROR, ApiMessage.getMessage("MSG001"));
		}
		return result;
	}

	@ApiOperation(value = "获取帖子评论接口", httpMethod = "POST")
	@PostMapping("listBbsComment")
	public ServiceResult<PageInfo<BbsComment>> listBbsComment(@ApiParam(value = "帖子Id") String bbsId) {
		ServiceResult<PageInfo<BbsComment>> result = new ServiceResult<>();
		try {
			result.setResultData(bbsCommentService.listBbsComment(bbsId));
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(ResultCode.REQUEST_ERROR, ApiMessage.getMessage("MSG001"));
		}
		return result;
	}


	@ApiOperation(value = "回复帖子评论接口", httpMethod = "POST")
	@PostMapping("addBbsComment")
	public ServiceResult<Boolean> addBbsComment(@RequestBody CommentVO vo) {
		ServiceResult<Boolean> result = new ServiceResult<>();
		try {
			bbsCommentService.addComment(vo);
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(ResultCode.REQUEST_ERROR, ApiMessage.getMessage("MSG001"));
		}
		return result;
	}

	@ApiOperation(value = "删除帖子评论接口", httpMethod = "POST")
	@PostMapping("delBbsComment")
	public ServiceResult<Boolean> delBbsComment(@ApiParam(value = "评论Id") Integer id) {
		ServiceResult<Boolean> result = new ServiceResult<>();
		try {
			bbsCommentService.delComment(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(ResultCode.REQUEST_ERROR, ApiMessage.getMessage("MSG001"));
		}
		return result;
	}

	@ApiOperation(value = "点赞帖子接口", httpMethod = "POST")
	@PostMapping("likeBbs")
	public ServiceResult<Boolean> likeBbs(@ApiParam(value = "帖子Id") Integer bbsId) {
		ServiceResult<Boolean> result = new ServiceResult<>();
		try {
			yuncoolBbsService.likeBbs(bbsId);
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(ResultCode.REQUEST_ERROR, ApiMessage.getMessage("MSG001"));
		}
		return result;
	}

	@ApiOperation(value = "取消赞帖子接口", httpMethod = "POST")
	@PostMapping("noLikeBbs")
	public ServiceResult<Boolean> noLikeBbs(@ApiParam(value = "帖子Id") Integer bbsId) {
		ServiceResult<Boolean> result = new ServiceResult<>();
		try {
			yuncoolBbsService.noLikeBbs(bbsId);
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(ResultCode.REQUEST_ERROR, ApiMessage.getMessage("MSG001"));
		}
		return result;
	}

	@ApiOperation(value = "增加浏览量接口", httpMethod = "POST")
	@PostMapping("viewBbs")
	public ServiceResult<Boolean> viewBbs(@ApiParam(value = "帖子Id") Integer bbsId) {
		ServiceResult<Boolean> result = new ServiceResult<>();
		try {
			yuncoolBbsService.viewBbs(bbsId);
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(ResultCode.REQUEST_ERROR, ApiMessage.getMessage("MSG001"));
		}
		return result;
	}
}
